import jwt


# from conf.comm import SECRET_KEY
class MyJwt:
    def __init__(self):
        self.SECRET_KEY = 'asfsf!#$#$@$#^%#$sdfwtwa134342432323fffwf'

    # 生成jwt
    def jwt_encode(self, data):
        token = jwt.encode(data, self.SECRET_KEY, algorithm='HS256')
        return str(token, encoding='utf8')

    # 解密jwt
    def jwt_decode(self, token):
        try:
            data = jwt.decode(token, self.SECRET_KEY, algorithm='HS256')
            # 重新加密获取新的token
            newtoken = jwt.encode(data, self.SECRET_KEY, algorithm='HS256')
            return str(newtoken, encoding='utf8')
        except:
            return None

    # 对比
    def check(self, token, newtoken):
        if token == newtoken:
            return True
        return False


myjwt = MyJwt()
# token = myjwt.jwt_encode({'data':{'name':'sdf'}})

# newtoken = myjwt.jwt_decode('233242424')
# flag = myjwt.check(token,newtoken)
# print(flag)
